package com.wen.prototype;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试浅拷贝的问题
 */
public class TestShallowCopy {

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("张三");
        A a = new A(list);
        A clone = a.clone();
        clone.getList().add("李四");
        for (String s : a.getList()) {
            System.out.println(s);
        }
    }

    /**
     * cloneable  接口实现是为了给jvm提供一个标识  来表示这个类是可以在堆内存中进行复制的
     */
   static class A implements Cloneable{
        protected List<String> list;
        public A() {
        }
        public A(List<String> list) {
            this.list = list;
        }
        public List<String> getList() {
            return list;
        }
        public void setList(List<String> list) {
            this.list = list;
        }
        @Override
        public A clone() {
            A a = null;
            try {
                a = (A) super.clone();
            } catch (CloneNotSupportedException e) {
                e.printStackTrace();
            }
            return a;
        }
    }

}
